package cn.wq3stone.web;

import cn.wq3stone.exception.LoginException;
import cn.wq3stone.exception.UsernameNullException;
import cn.wq3stone.pojo.User;
import cn.wq3stone.service.UserService;
import cn.wq3stone.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * @author cn.wq3stone
 */

public class LoginServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        PrintWriter out = resp.getWriter();
        try {
            User user = userService.login(username, password);
            out.println("Welcome " + user.getUsername() + " login the system测试");
        } catch (UsernameNullException e) {
            out.println("user is not exist测试");
        } catch (LoginException e) {
            out.println("username or password is wrong测试");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
